package org.nokatag.dbgrid;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nokatag.system.BugInfoWrint;
import org.nokatag.system.DBCon;
/**
 * DBGrid工具类
 * @author rebin
 */
public class NDBGridUtil {
	/**
	 * 判断模糊查询
	 * @param s
	 * @param v
	 * @return
	 */
	public  static Integer MQuery(String s,String v){//s = and 测试   = $[sfffs,sff,minput]
		//----------------------------------------------------------------
		if((v.indexOf(",multi")!=-1 || v.indexOf(",tree")!=-1 || v.indexOf(",treetran")!=-1 || v.indexOf(",mtreetran")!=-1 || v.indexOf(",mtext")!=-1 || v.indexOf(",minput")!=-1))
		{
			if(s.indexOf("%") ==-1 ){
				return 4;
			}else if(s.indexOf("%"+v+"%")!=-1)
				return 5;
			else if(s.indexOf("%"+v)!=-1)
				return 6;
			else if(s.indexOf(v+"%")!=-1)
				return 7;
		}
		//----------------------------------------------------------------
		if(s.indexOf("}")!=-1 && s.indexOf("%"+v+"%")!=-1)
			return 8;
		else if(s.indexOf("}")!=-1 && s.indexOf("%"+v)!=-1)
			return 9;
		else if(s.indexOf("}")!=-1 && s.indexOf(v+"%")!=-1)
			return 10;
		else if(s.indexOf("}")!=-1 && s.indexOf(v)!=-1)
			return 11;
		//----------------------------------------------------------------
		if(s.indexOf("%"+v+"%")!=-1)
			return 1;
		else if(s.indexOf("%"+v)!=-1)
			return 2;
		else if(s.indexOf(v+"%")!=-1)
			return 3;
		else
			return 0;
		//----------------------------------------------------------------
	}
	/**
	 * 取指定对象
	 * @return
	 */
	public static CellParItem getCellType(HttpServletRequest request,String sqls,String cellName){
		PreparedStatement pas=  null;
		try{
			Connection con=DBCon.getCon();//sql语句
			if(sqls.indexOf("$[where]")!=-1){
				sqls=sqls.replace("$[where]", " and 1<>1");
			}
			if(sqls.indexOf("$[page]")!=-1){
				sqls=sqls.replace("$[page]", "");
			}
			if(null!=con){
				pas= con.prepareStatement(sqls,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
				ResultSetMetaData rsmd = pas.getMetaData();// 取得列名
				for(int i=1;i<=rsmd.getColumnCount();i++){
					if(cellName.equalsIgnoreCase(rsmd.getColumnName(i).trim())){
						return new CellParItem(rsmd.getColumnType(i),rsmd.getColumnName(i).trim());
					}
				}
			}
		}catch(Exception se){
			
		}finally{
			if(null!=pas){
				try{pas.close();}catch(Exception e){}
			}
		}
		return null;
	}
	/**
	 * 取列名
	 * @return
	 */
	public static List<CellParItem> getCellType(HttpServletRequest request,String sqls){
		List<CellParItem> m= new ArrayList<CellParItem>();
		PreparedStatement pas=  null;
		try{
			Connection con=DBCon.getCon();//sql语句
			if(sqls.indexOf("$[where]")!=-1){
				sqls=sqls.replace("$[where]", "");
			}
			if(sqls.indexOf("$[page]")!=-1){
				sqls=sqls.replace("$[page]", "");
			}
			if(null!=con){
				pas= con.prepareStatement(sqls);
				ResultSetMetaData rsmd = pas.getMetaData();// 取得列名
				for(int i=1;i<=rsmd.getColumnCount();i++){
					m.add(i-1,new CellParItem(rsmd.getColumnType(i),rsmd.getColumnName(i).trim(),rsmd.getColumnName(i).trim()));//列名及类型
				}
			}
		}catch(Exception se){
			BugInfoWrint.Print(se);
		}finally{
			if(null!=pas){
				try{pas.close();}catch(Exception e){}
			}
		}
		return m;
	}
	/**
	 * 查找数据字段类型
	 * @param cel
	 * @param s
	 * @return
	 */
	public static Integer getType(List<CellParItem> cel,String s){
		for(CellParItem c:cel){
			if(s.indexOf(c.getCellname())!=-1)
				return c.getType();
		}
	    return java.sql.Types.VARCHAR;
	}
}
